package com.mxx.common.netty.http;

import com.mxx.common.netty.NettyHttpOperator;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/6/29 11:11
 * @Description: http处理器
 */

@Slf4j
@ChannelHandler.Sharable
public class NettyHttpHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    private NettyHttpOperator nettyHttpOperator;

    public NettyHttpHandler(NettyHttpOperator nettyHttpOperator) {
        this.nettyHttpOperator = nettyHttpOperator;
    }

    /**
     *
     * @param ctx
     */

    /**
     * 数据读取完成
     *
     * @param ctx
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    /**
     * 开始读取数据
     *
     * @param ctx
     * @param request
     */

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
        nettyHttpOperator.doRead(ctx, request.copy());
    }


    /**
     * 异常处理
     *
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        nettyHttpOperator.exceptionCaught(ctx, cause);
    }


}